import ij.ImagePlus;
import ij.plugin.PlugIn;
import ij.process.ColorProcessor;
import ij.process.ImageProcessor;


import java.awt.Color;


import java.awt.Font;

import java.awt.Polygon;
import java.io.PrintStream;

import java.util.Locale;
import java.util.Random; 

import javax.swing.JFileChooser;



import pluginUtils.DEMReader;
import pluginUtils.HGTReader;
import pluginUtils.Point2D;

public class DrawingLandscape extends ImagePlus implements PlugIn{
	{Locale.setDefault(Locale.US); }
	
	private ImageProcessor primeIP; 
	private int X=2000,Y=2000; // image size
	
	
	private double xShift=0; 
	private double zShift=0; 
	
	

	private Random generator=new Random(); // One always needs some randomness
	
	// For the scaling of the image
	private double x0=0,x1=1,y0=0,y1=1;
	private double acoef,ccoef,vcoef,wcoef; 
	
	
	public void setCoefs(){
		acoef=X/(x1-x0); 
		vcoef=Y/(y1-y0); 
		ccoef=-acoef*x0; 
		wcoef=-vcoef*y0; 
	}
	 
	
	
	public void setBounds(double xl,double xu,double yl,double yu){
		x0=xl; 
		x1=xu;
		y0=yl;
		y1=yu; 
	}
	
	private void drawCircle(Point2D C,double r){
		double xCenter=C.getX(); 
		double yCenter=C.getY(); 
		
		primeIP.setColor(Color.BLUE); 
		//drawPoint(xCenter,-yCenter);
		primeIP.setColor(Color.BLACK);
		
		int intX=(int)((xCenter)*acoef+ccoef);
		int intY=(int)((yCenter)*vcoef+wcoef);
		int intRx=(int)(r*acoef);
		int intRy=(int)(r*vcoef);
		
		
		primeIP.drawOval(intX-intRx,intY-intRy,intRx*2,intRy*2);
	}
	
	private void drawPoint(double x,double y){
		int radi=1; 
		int intX=(int)(x*acoef+ccoef);
		int intY=(int)(y*vcoef+wcoef);
		primeIP.fillOval(intX-radi,intY-radi,2*radi,2*radi);
	}
	
	private void drawPoint(Point2D p){
		int intX=(int)(p.getX()*acoef+ccoef);
		int intY=(int)(p.getY()*vcoef+wcoef);
		primeIP.fillOval(intX-10,intY-10,20,20);
	}
	
	
	
	
	private void drawLine(double xd,double yd,double xf,double yf){
		int XD=(int)(xd*acoef+ccoef); 
		int YD=(int)(yd*vcoef+wcoef); 
		int XF=(int)(xf*acoef+ccoef); 
		int YF=(int)(yf*vcoef+wcoef);
		primeIP.drawLine(XD,YD,XF,YF); 
	}
	
	
	private void drawLine(Point2D p,Point2D q){
		int XD=(int)(p.getX()*acoef+ccoef); 
		int YD=(int)(p.getY()*vcoef+wcoef); 
		int XF=(int)(q.getX()*acoef+ccoef); 
		int YF=(int)(q.getY()*vcoef+wcoef);
		primeIP.drawLine(XD,YD,XF,YF); 
	}
	

	
	public void run(String arg) {
		primeIP = new ColorProcessor(X,Y);
	
		double limit=0.6; 
		double lmoins=-0.6;
		
		setBounds(0,1201,0,1201);
		setCoefs(); 
		
		 JFileChooser chooser = new JFileChooser();
		 chooser.setFileFilter(chooser.getAcceptAllFileFilter());
		 int returnVal = chooser.showOpenDialog(null);
		 if (returnVal == JFileChooser.APPROVE_OPTION) 

		 {
		   String chosen=chooser.getSelectedFile().getAbsolutePath();
		   System.out.println("You have chosen to open this file: " + chosen);
		 }
		
		
		Polygon background=new Polygon(new int[]{0,0,X,Y},new int[]{0,X,Y,0},4);
		primeIP.setColor(Color.WHITE);
		
		primeIP.fillPolygon(background);
		
		
		primeIP.setFont(new Font(null,Font.ITALIC,60));
		primeIP.setColor(Color.BLACK);
		
		setProcessor("Verifying", primeIP);
		show(); 
		primeIP.setLineWidth(1);
		
		HGTReader toto=new HGTReader(); 
		//toto.readData("C:/Users/decomite/Downloads/S38W013.hgt/S38W013.hgt");
		toto.readData(chooser.getSelectedFile().getAbsolutePath()); 
		int datas[][]=toto.getValeurs(); 
		System.out.println("min : "+toto.getMinElevationInt()+" max "+toto.getMaxElevationInt()); 
		int min=toto.getMinElevationInt(); 
		int max=toto.getMaxElevationInt(); 
		for(int i=0;i<1201;i++){
			for(int k=0;k<1201;k++){
				float dif=(0.0f+datas[i][k]-min)/(0.0f+max-min); 
				primeIP.setColor(new Color(dif,dif,dif)); 
					drawPoint(i+0.0,k+0.0);
						
			}
			if(i%100==0) updateAndDraw(); 
		
		}
	
		show();
	}	
}
